
/*
 * Copyright (C) 2009 Archie L. Cobbs. All rights reserved.
 *
 * $Id: LongField.java 377 2010-03-11 20:50:18Z archie.cobbs $
 */

package org.dellroad.sidekar;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import org.dellroad.sidekar.util.PackedLong;
import org.dellroad.stuff.string.ParseContext;

/**
 * Long field.
 *
 * @see org.dellroad.sidekar.annotation.SidekarField
 */
public final class LongField extends AtomicField<Long> {

    LongField(String name, Entity<?> entity) {
        super(name, entity, Long.class);
    }

    @Override
    public void visit(FieldSwitch fieldSwitch) {
        fieldSwitch.caseLongField(this);
    }

    @Override
    public void encode(DataOutputStream output, Long value) throws IOException {
        PackedLong.write(output, value);
    }

    @Override
    public Long decode(DataInputStream input) throws IOException {
        return PackedLong.read(input);
    }

    @Override
    public String toString(Long value) {
        return value.toString();
    }

    @Override
    public Long fromString(ParseContext ctx) {
        return Long.parseLong(ctx.matchPrefix("-?[0-9]+").group());
    }
}

